<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('workOrder.userName')" prop="userId">
        <el-input
          v-model="queryParams.userId"
          :placeholder="$t('workOrder.userName')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ems:workorderDevops:add']"
        >{{$t('common.add')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ems:workorderDevops:edit']"
        >{{$t('common.modify')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workorderDevopsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('workOrder.userName')" align="center" prop="userName" />
      <el-table-column :label="$t('workOrder.phoneNumber')" align="center" prop="phoneNumber" />
      <el-table-column :label="$t('workOrder.state')" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.operator')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ems:workorderDevops:edit']"
          >{{$t('common.modify')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ems:workorderDevops:remove']"
          >{{$t('common.delete')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维护人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item :label="$t('workOrder.user')" prop="userId">
          <el-input v-model="form.userName" :placeholder="$t('workOrder.user')" readonly>
            <template slot="append" v-if="form.id==null">
              <el-button size="mini" type="text" @click="onOpen" style="padding:0 15px 0 15px;">{{$t('workOrder.selectUser')}}</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('workOrder.phoneNumber')" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" :placeholder="$t('workOrder.phoneNumber')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('workOrder.state')">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.common_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('common.confirm')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>

    <select-user @ok="onSelect" ref="selectUser" ></select-user>
  </div>
</template>

<script>
import { listWorkorderDevops, getWorkorderDevops, delWorkorderDevops, addWorkorderDevops, updateWorkorderDevops } from "@/api/ems/workorderDevops";
import selectUser from "../../system/user/selectUser";
export default {
  components: { selectUser },
  name: "WorkorderDevops",
  dicts: ['common_status'],
  component:[selectUser],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维护人员表格数据
      workorderDevopsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: this.$t('common.cannotbeempty',{name:this.$t('workOrder.user')}), trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维护人员列表 */
    getList() {
      this.loading = true;
      listWorkorderDevops(this.queryParams).then(response => {
        this.workorderDevopsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        phoneNumber:null ,
        userName:null ,
        state: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('workOrder.addStaff');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkorderDevops(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('workOrder.editStaff');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkorderDevops(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.successful'));
              this.open = false;
              this.getList();
            });
          } else {
            addWorkorderDevops(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.successful'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('common.deleteData',{name:row.name})).then(function() {
        return delWorkorderDevops(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.successful'));
      }).catch(() => {});
    },
    onOpen(){
      this.$refs["selectUser"].show()
    },
    /** 导出按钮操作 */
    onSelect(obj) {
      console.log(obj)
      if(obj) {
        this.form.userId = obj[0].userId
        this.form.phoneNumber = obj[0].phonenumber
        this.form.userName = obj[0].nickName
      }
    }
  }
};
</script>
